//
//  UIPageControlView.m
//  FanXing
//
//  Created by defoe on 16/4/25.
//  Copyright © 2016年 kugou. All rights reserved.
//

#import "FXPageControlView.h"

@interface FXPageControlView () {
    
}

@property(nonatomic, strong) NSMutableArray *imageViewArray;

@end


@implementation FXPageControlView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.imageViewArray = [NSMutableArray array];
    }
    return self;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat normalImageWidth = self.normalImage.size.width;
    CGFloat lightImageWidth = self.lightImage.size.width;
    CGFloat imageHeight = self.lightImage.size.height;
    
    CGFloat widthCount = (self.numberOfPages - 1) * normalImageWidth + lightImageWidth + (self.numberOfPages - 1) * self.itemGap;
    CGFloat itemX = SizeCenter(self.width, widthCount);
    CGFloat itemY = SizeCenter(self.height, imageHeight);
    for (NSInteger i = 0; i < [self.imageViewArray count]; i++) {
        UIImageView *imageView = [self.imageViewArray objectAtIndex:i];
        CGFloat imageWitdth = 0;
        if (i == self.currentPage) {
            imageView.image = self.lightImage;
            imageWitdth = lightImageWidth;
        } else {
            imageView.image = self.normalImage;
            imageWitdth = normalImageWidth;
        }
        imageView.frame = CGRectMake(itemX, itemY, imageWitdth, imageHeight);
        imageView.layer.cornerRadius = imageView.height/2;
        imageView.clipsToBounds = YES;
        itemX += (imageWitdth + self.itemGap);
    }
}

- (void)setNumberOfPages:(NSUInteger)numberOfPages {
    _numberOfPages = numberOfPages;
    
    for (UIView *view in self.imageViewArray) {
        [view removeFromSuperview];
    }
    [self.imageViewArray removeAllObjects];
    
    for (NSInteger i = 0; i < numberOfPages; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        [self addSubview:imageView];
        [self.imageViewArray addObject:imageView];
    }
    
    [self setNeedsLayout];
}

- (void)setCurrentPage:(NSUInteger)currentPage {
    _currentPage = currentPage;
    [self setNeedsLayout];
}

- (void)setPageCorRadius{
    
}
@end
